#!/bin/bash

. $TEST_ROOT_DIR/run-test-common

set -x

if [ "$OS" != "Windows_NT" ]; then
    echo 'This UWP test only runs on Windows.'
    exit 1
fi

# This test uses a large dataset which is not part of the CNTK repository itself
# We use the dataset from an external location specified using an environment variable
if [[ "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" == "" || ! -d "$CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY" ]]; then
  echo 'This test uses external data that is not part of the CNTK repository. Environment variable CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY must be set to point to the external test data location'
  exit 1
fi

TEST_DATA_DIR=$(cygpath -au $CNTK_EXTERNAL_TESTDATA_SOURCE_DIRECTORY)

VSTEST_CONSOLE=$(cygpath -au "$VS2017INSTALLDIR\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\vstest.console.exe")

test -x "$VSTEST_CONSOLE" || exit $?

# Use pretrained model
MODEL_DIR=$(cygpath -au "$USERPROFILE\\Documents")
cp $TEST_DATA_DIR/PreTrainedModels/EvalModels/01_OneHidden.model "$MODEL_DIR"/01_OneHidden.model || exit $?
cp $TEST_DATA_DIR/PreTrainedModels/EvalModels/resnet20_cifar10_python.dnn "$MODEL_DIR"/resnet20.model || exit $?
cp $TEST_DATA_DIR/PreTrainedModels/EvalModels/atis.dnn "$MODEL_DIR"/atis.model || exit $?
cp $TEST_ROOT_DIR/../../Examples/LanguageUnderstanding/ATIS/BrainScript/query.wl "$MODEL_DIR"/query.wl || exit $?
cp $TEST_ROOT_DIR/../../Examples/LanguageUnderstanding/ATIS/BrainScript/slots.wl "$MODEL_DIR"/slots.wl || exit $?

# Set CUDA_VISIBLE_DEVICES to exclude all gpu if running on cpu device
[ "$TEST_DEVICE" == "cpu" ] && export CUDA_VISIBLE_DEVICES=-1

"$VSTEST_CONSOLE" /inIsolation /Platform:x64 $(cygpath -aw "$TEST_BIN_DIR/CNTKLibraryCPPUWPEvalExamplesTests/AppPackages/CNTKLibraryCPPUWPEvalExamplesTests_1.0.0.0_x64_"$TEST_FLAVOR"_UWP_Test/CNTKLibraryCPPUWPEvalExamplesTests_1.0.0.0_x64_"$TEST_FLAVOR"_UWP.appx") || exit $?

 
for fn in 01_OneHidden.model resnet20.model.model atis.model query.wl slots.wl; do
  rm -rf "$MODEL_FILE"/$fn || exit $?
  if [[ -f "$MODEL_FILE"/$fn ]]; then
    echo "Failed to delete the model file $MODEL_FILE"
    exit 1
  fi
done

exit 0
